package com.rhb.rhbaiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.rhb.rhbaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * 文件操作工具类
 */
public class FileOperationTool {
    private final String fileDir = FileConstant.File_Save_Dir + "/file";

    @Tool(description = "Read content from a file")
    public String readFile(@ToolParam(description = "Name of file to read") String FileName){
        String filePath = fileDir + "/"  + FileName;
        try {
            return FileUtil.readUtf8String(filePath);
        }catch (Exception e){
            return "Error Reading file: " + e.getMessage();
        }
    }

    @Tool(description = "Write content to a file")
    public String writeFile(
            @ToolParam(description = "Name of the file to write") String fileName,
            @ToolParam(description = "Content to write to the file") String content
    ){
        String filePath = fileDir + "/"  + fileName;
        try{
            FileUtil.mkdir(fileDir);
            FileUtil.writeUtf8String(content, filePath);
            return  "File written successfully to" + filePath;
        }catch (Exception e){
            return "Error writting successfully to file" +  e.getMessage();
        }

    }
}
